home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / export / outbound / common / outbound.c next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  6.5 KB  |  314 lines

  1. /*
  2.     File: Outbound.c
  3.  
  4.     Copyright (c) 1990-1, Thomas Knoll.
  5.     Copyright (c) 1992-6, Adobe Systems Incorporated.
  6.     All rights reserved.
  7.  
  8.     C source file for Outbound export example.
  9. */
  10.  
  11. #if __MWERKS__
  12. #include <SetupA4.h> // A4-globals
  13. #include <A4Stuff.h> // A4-globals    
  14. #endif
  15.  
  16. #if defined(THINK_C) || defined(__MWERKS__)
  17. #define ENTRYPOINT main
  18. #endif
  19.  
  20. #include "Outbound.h"
  21.  
  22. Handle hDllInstance = NULL;
  23.  
  24. /*****************************************************************************/
  25.  
  26. void InitGlobals (GPtr globals);
  27. void DoPrepare (GPtr globals);
  28. void DoStart (GPtr globals);
  29. void DoContinue (GPtr globals);
  30. void DoFinish (GPtr globals);
  31. void DoInitialRect (GPtr globals);
  32. Boolean DoNextRect (GPtr globals);
  33. void DoExportRect (GPtr globals);
  34.  
  35. /*****************************************************************************/
  36.  
  37. /* All calls to the plug-in module come through this routine. It must be
  38.    placed first in the resource. To achieve this, most development systems
  39.    require that this be the first routine in the source. */
  40.  
  41. #if MSWindows
  42. void ENTRYPOINT (short selector,
  43.                         ExportRecord *exportParamBlock,
  44.                         long *data,
  45.                         short *result)
  46. #else
  47. pascal void ENTRYPOINT (short selector,
  48.                         ExportRecord *exportParamBlock,
  49.                         long *data,
  50.                         short *result)
  51. #endif
  52.     {
  53.     
  54.     Globals globalValues;
  55.     GPtr globals = &globalValues;
  56.     
  57.     #if __MWERKS__
  58.     EnterCodeResource(); // A4-globals
  59.     #endif    
  60.     
  61.     if (!*data)
  62.         {
  63.         
  64.         InitGlobals (globals);
  65.  
  66.         *data = (long) NewHandle (sizeof (Globals));
  67.         
  68.         if (!*data)
  69.             {
  70.             *result = memFullErr;
  71.             return;
  72.             }
  73.             
  74.         ** (GHdl) *data = globalValues;
  75.         
  76.         }
  77.         
  78.     globalValues = ** (GHdl) *data;
  79.         
  80.     gStuff = exportParamBlock;
  81.     gResult = noErr;
  82.         
  83.     switch (selector)
  84.         {
  85.         
  86.         case exportSelectorAbout:
  87.             DoAbout (globals);
  88.             break;
  89.             
  90.         case exportSelectorPrepare:
  91.             DoPrepare (globals);
  92.             break;
  93.         
  94.         case exportSelectorStart:
  95.             DoStart (globals);
  96.             break;
  97.         
  98.         case exportSelectorContinue:
  99.             DoContinue (globals);
  100.             break;
  101.         
  102.         case exportSelectorFinish:
  103.             DoFinish (globals);
  104.             break;
  105.             
  106.         default:
  107.             gResult = exportBadParameters;
  108.         
  109.         }
  110.         
  111.     *result = gResult;
  112.     ** (GHdl) *data = globalValues;
  113.  
  114.     #if __MWERKS__
  115.     ExitCodeResource(); // A4-globals
  116.     #endif
  117.         
  118.     }
  119.  
  120. /*****************************************************************************/
  121.  
  122. void InitGlobals (GPtr globals)
  123. {
  124.     
  125.     gQueryForParameters = true;
  126.     
  127. }
  128.  
  129. /*****************************************************************************/
  130.  
  131. /* Prepare to export an image.    If the plug-in module needs only a limited
  132.    amount of memory, it can lower the value of the 'maxData' field. */
  133.  
  134. void DoPrepare (GPtr globals)
  135.     {
  136.     
  137.     if (gStuff->maxData > 0x80000)
  138.         gStuff->maxData = 0x80000;
  139.     
  140.     }
  141.  
  142. /*****************************************************************************/
  143.  
  144. /* Requests pointer to the first part of the image to be filtered. */
  145.  
  146. void DoStart (GPtr globals)
  147.     {
  148.     
  149.     FileHandle fRefNum;
  150.     
  151.     /* This plug-in does not support bitmap images */
  152.  
  153.     if (gStuff->imageMode == plugInModeBitmap)
  154.         {
  155.         gResult = exportBadMode;
  156.         return;
  157.         }
  158.     
  159.     /* check with the scripting system whether to pop our dialog */
  160.     gQueryForParameters = true; // assume yes
  161.     gAliasHandle = nil; // no handle, yet
  162.     
  163.     ReadScriptParams (globals);
  164.     
  165.     if (!QueryForExportFile (globals)) return;
  166.         
  167.     if (!CreateExportFile (globals, &fRefNum)) return;
  168.     
  169.     (void) WriteExportFile (globals, fRefNum);
  170.     
  171.     (void) CloseExportFile (globals, fRefNum);
  172.     
  173.     MarkExportFinished (gStuff);
  174.  
  175.     }
  176.  
  177. /*****************************************************************************/
  178.  
  179. /* Filters the area and requests the next area. */
  180.  
  181. void DoContinue (GPtr globals)
  182.     {
  183.     
  184.     /* We shouldn't get here because we did all of the work during the
  185.        start phase, but we add some code just in case. */
  186.     
  187.     MarkExportFinished (gStuff);
  188.     
  189.     gResult = userCanceledErr;
  190.  
  191.     }
  192.  
  193. /*****************************************************************************/
  194.  
  195. /* This routine will always be called if DoStart does not return an error
  196.    (even if DoContinue returns an error or the user aborts the operation).
  197.    This allows the module to perform any needed cleanup.  None is required
  198.    in this example. */
  199.  
  200. void DoFinish (GPtr globals)
  201. {
  202.     WriteScriptParams (globals);
  203. }
  204.  
  205. /*****************************************************************************/
  206.  
  207. #define RANGE_ITER(lower,upper,first,last,step)                                  \
  208.     for (lower = (first);                                                       \
  209.          (upper = (((lower) + (step) < (last)) ? (lower) + (step) : (last))), \
  210.          lower < (last);                                                      \
  211.          lower = upper)
  212.  
  213. /*****************************************************************************/
  214.  
  215. Boolean WriteExportFile (GPtr globals, FileHandle fRefNum)
  216. {
  217.     /* We write out the file as an interleaved raw file. */ 
  218.     
  219.     /* We need to figure out how many rows to write at one time. */
  220.     
  221.     long chunk = gStuff->maxData / gStuff->imageSize.h / gStuff->planes;
  222.     
  223.     ExportRegion region;
  224.     
  225.     region.rect.left = 0;
  226.     region.rect.right = gStuff->imageSize.h;
  227.     region.loPlane = 0;
  228.     region.hiPlane = gStuff->planes - 1;
  229.     
  230.     RANGE_ITER (region.rect.top, region.rect.bottom,
  231.                 0, gStuff->imageSize.v, chunk)
  232.         {
  233.         
  234.         int16 row;
  235.         long rowCount = gStuff->imageSize.h * (long) gStuff->planes;
  236.         void *data = 0;
  237.         int32 rowBytes = 0;
  238.         unsigned8 *rowData;
  239.         
  240.         if (!TSC (TestAbort ())) return FALSE;
  241.             
  242.         if (!TSR (FetchData (gStuff, ®ion, &data, &rowBytes))) return FALSE;
  243.         
  244.         for (row = region.rect.top, rowData = (unsigned8 *) data;
  245.              row < region.rect.bottom;
  246.              ++row, rowData += rowBytes)
  247.             {
  248.             
  249.             long count = rowCount;
  250.             
  251.             UpdateProgress (row, gStuff->imageSize.v);
  252.             
  253.             if (!TSC (TestAbort ())) return FALSE;
  254.             
  255.             if (!TSR (FSWrite (fRefNum, &count, rowData))) return FALSE;
  256.             
  257.             }
  258.         
  259.         }
  260.         
  261.     return TRUE;
  262.     
  263. }
  264.  
  265. /*****************************************************************************/
  266.  
  267. OSErr FetchData (ExportRecord *stuff,
  268.                  ExportRegion *region,
  269.                  void **data,
  270.                  int32 *rowBytes)
  271.     {
  272.     
  273.     OSErr result;
  274.     
  275.     if (!WarnHostAdvanceStateAvailable (stuff->advanceState, hDllInstance))
  276.         return userCanceledErr;
  277.         
  278.     stuff->theRect = region->rect;
  279.     stuff->loPlane = region->loPlane;
  280.     stuff->hiPlane = region->hiPlane;
  281.     
  282.     result = (*(stuff->advanceState)) ();
  283.     
  284.     if (result != noErr)
  285.         {
  286.         *data = NULL;
  287.         *rowBytes = 0;
  288.         }
  289.     else
  290.         {
  291.         *data = stuff->data;
  292.         *rowBytes = stuff->rowBytes;
  293.         }
  294.         
  295.     return result;
  296.     
  297.     }
  298.  
  299. /*****************************************************************************/
  300.  
  301. void MarkExportFinished (ExportRecord *stuff)
  302.     {
  303.     
  304.     PISetRect (&stuff->theRect, 0, 0, 0, 0);
  305.     
  306.     }
  307.  
  308. /*****************************************************************************/
  309.  
  310.  
  311.  
  312.  
  313.  
  314.